﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using Frame.Lib.Core.Tree;
using Frame.Lib.Common;

namespace Frame.Lib.Utilities
{
    public class LVUtil
    {

        public static void Init(System.Web.UI.WebControls.ListView lv, System.Web.UI.HtmlControls.HtmlTableRow lvHeader)
        {
            lv.EnableViewState = false;
            lv.ItemDataBound += new EventHandler<System.Web.UI.WebControls.ListViewItemEventArgs>(lv_ItemDataBound);
            CheckBox chkSelectAll = (CheckBox)lvHeader.FindControl("chkSelectAll");
            lvHeader.Cells[lvHeader.Cells.Count - 1].Attributes["class"] = "last";
            if (chkSelectAll != null)
            {
                chkSelectAll.Attributes.Add("onclick", "SelectAll(document.getElementById('lvList'))");
            }
        }

        static void lv_ItemDataBound(object sender, System.Web.UI.WebControls.ListViewItemEventArgs e)
        {
            ListView lv = (ListView)sender;
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                CheckBox chkSelect = (CheckBox)e.Item.FindControl("chkSelect");
                HiddenField hidSelectIDs = (HiddenField)lv.Parent.FindControl("hidSelectIDs");
                string strID = lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString();

                if (chkSelect != null)
                {
                    //给行加上单击选中事件                   
                    foreach (Control ctl in e.Item.Controls)
                    {
                        if (ctl is HtmlTableRow)
                        {
                            HtmlTableRow row = (HtmlTableRow)ctl;
                            row.Attributes.Add("onclick", "SelectTr('" + chkSelect.ClientID + "');");
                            if (row.Attributes["onmouseover"] == null)
                            {
                                row.Attributes.Add("onmouseover", "oldbg=this.className;this.className='trover';");
                                row.Attributes.Add("onmouseout", "this.className=oldbg;");
                            }
                        }
                    }

                    if (hidSelectIDs != null)
                    {
                        chkSelect.Attributes.Add("onclick", "SetSelectID('" + hidSelectIDs.ClientID + "','" + strID + "')");
                    }
                }
                TextBox txtOrders = (TextBox)e.Item.FindControl("txtOrders");//处理排序
                if (txtOrders != null)
                {
                    HiddenField hidOrders = (HiddenField)lv.Parent.FindControl("hidOrders");
                    txtOrders.Attributes.Add("onchange", "SetOrders(document.getElementById('" + hidOrders.ClientID + "')," + strID + ",this)");
                    txtOrders.Attributes.Add("onmouseover", "this.className = 'over'");
                    txtOrders.Attributes.Add("onmouseout", "this.className = 'out'");
                    txtOrders.Attributes.Add("onfocus", "this.className = 'focus'");
                    txtOrders.Attributes.Add("onblur", "ValidateOrders(this)");
                }
            }
        }

        public static void RestoreTreeListState(ListView lv, string strHideIDs)
        {
            List<string> lstIDFlags = StrUtil.SplitToStr(strHideIDs);
            Dictionary<int, string> htIDs = new Dictionary<int, string>();//把ID和flag分开

            foreach (string str in lstIDFlags)
            {
                string[] tmp = str.Split('|');
                htIDs.Add(int.Parse(tmp[0]), tmp[1]);
            }
            for (int i = 0; i < lv.Items.Count; i++)
            {
                int id = (int)lv.DataKeys[i]["ID"];
                if (htIDs.ContainsKey(id))
                {
                    HtmlTableRow row = null;
                    foreach (Control ctl in lv.Items[i].Controls)
                    {
                        if (ctl is HtmlTableRow)
                        {
                            row = (HtmlTableRow)ctl;
                            break;
                        }
                    }

                    row.Style.Add("display", "none");
                    row.Attributes.Add("flag", htIDs[id]);
                }

                if (SubRowIsHide(i, htIDs, lv))//判断它是否有子行，如有则判断子行是否显示，如不显示则图片变为“+”号
                {
                    Literal litSpace = (Literal)lv.Items[i].FindControl("litSpace");
                    litSpace.Text = litSpace.Text.Replace("-.jpg", "+.jpg");
                }
            }
        }
        private static bool SubRowIsHide(int rowIndex, Dictionary<int, string> htIDs, ListView lv)
        {
            bool flag = false;
            System.Collections.IEnumerable dv = (System.Collections.IEnumerable)lv.DataSource;

            int id = (int)lv.DataKeys[rowIndex]["ID"];

            int count = 0;
            int firstSubID = 0;
            foreach (TreeInfo tree in dv)
            {
                if (tree.UpperID == id)
                {
                    count++;
                    if (firstSubID == 0)
                    {
                        firstSubID = tree.ID;
                    }
                }
            }

            if (count > 0)
            {
                if (htIDs.ContainsKey(firstSubID))
                {
                    flag = true;
                }
            }

            return flag;
        }

        public static void SetEdit(object sender, ListViewItemEventArgs e, int width, int height, string strEditUrl)
        {
            ListView lv = (ListView)sender;
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                string strID = StrUtil.EncryptUrl(lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString());
                HyperLink lnkName = (HyperLink)e.Item.FindControl("lnkName");
                HyperLink lnkEdit = (HyperLink)e.Item.FindControl("lnkEdit");
                if (string.IsNullOrEmpty(strEditUrl))
                {
                    strEditUrl = GlobalVar.WebCurrPath + "Edit.aspx";
                }

                if (strEditUrl.Contains("?"))
                {
                    strEditUrl += "&ID=" + strID;
                }
                else
                {
                    strEditUrl += "?ID=" + strID;
                }

                if (lnkName != null)
                {
                    lnkName.NavigateUrl = "javascript:;";

                    JSUtil.ShowForm(lnkName, "修改", null, strEditUrl, width, height);
                }
                if (lnkEdit != null)
                {
                    lnkEdit.NavigateUrl = "javascript:;";

                    JSUtil.ShowForm(lnkEdit, "修改", null, strEditUrl, width, height);
                }

            }
        }
        public static void SetEdit(object sender, ListViewItemEventArgs e, int width, int height)
        {
            SetEdit(sender, e, width, height, null);
        }
    }
}
